//
//  ComparisonSortingAlgorithmsView.swift
//  DataStructure
//
//  Created by kys-20 on 2019/8/27.
//  Copyright © 2019 kys-20. All rights reserved.
//

import Foundation
import UIKit
class ComparisonSortingAlgorithmsView: UIView {

    let mainQueue = DispatchQueue.main
    
    let randomizeArrayButton = UIButton() //随机化数组按钮
    let insertionSortButton = UIButton()//插入排序按钮
    let selectionSortButton = UIButton()//选择排序按钮
    let bubbleSortButton = UIButton()//冒泡排序按钮
    let quickSortButton = UIButton()//快速排序按钮
    let mergeSortButton = UIButton()//归并排序按钮
    let shellSortButton = UIButton()//希尔排序按钮
//    let changeSizeButton = UIButton()//改变尺寸按钮
    let slider = UISlider()//滑块 用来控制时间
    
    let labelArrayInterval = 5
    let labelArrayWIDTH = 20
    let labelArrayCount = (UIScreen.main.bounds.width - 5)/25
    
    var labelArray = [AccordingToHeightLabel]()
    var timeNum : Int = 0 //计时器次数
    var sumTime : Double = 1.00 // 总时间
    var outerLoopVariable = 0//外循环变量
    var withinLoopVariable = 0//内循环变量
    var time:Timer!//计时器
    var intermediateVariableLabel: AccordingToHeightLabel! // 中间变量Label
    var gap = 0 //希尔排序 作为间隔的值
    var shellnum = 0
    var newLabelArray = [AccordingToHeightLabel]()
    var parametersOfTheArray = [Double]()//用于归并排序的参数 包含三个值
    var signal: DispatchSemaphore!/////信号量处理事件并发 用来控制资源被多任务访问的情况
    
    let label = UILabel()//用于快速排序 标记基准
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    //    初始界面
    func setUpUI(){
        label.frame = CGRect(x: 0, y: -10, width: UIScreen.main.bounds.width, height: 1)
        label.backgroundColor = UIColor.red
        self.addSubview(label)
        
        slider.frame = CGRect(x: 10, y: UIScreen.main.bounds.height-110, width: UIScreen.main.bounds.width-20, height: 30)
        slider.minimumValue = 0
        slider.maximumValue = 4
        slider.value = 2.00
        self.addSubview(slider)
        slider.isContinuous = false//滑块滑动停止后才触发ValueChanged事件
        slider.addTarget(self, action: #selector(ActionSliderValueChange), for: .valueChanged)
        
        randomizeArrayButton.frame = CGRect(x: 10, y: 45, width: 120, height: 40)
        randomizeArrayButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        randomizeArrayButton.setTitle("Randomize Array", for: .normal)
        randomizeArrayButton.setTitleColor(UIColor.black, for: .normal)
        randomizeArrayButton.layer.masksToBounds = true
        randomizeArrayButton.layer.borderWidth = 1
        randomizeArrayButton.layer.borderColor = UIColor.black.cgColor
        randomizeArrayButton.tag = 0
        randomizeArrayButton.addTarget(self, action: #selector(randomizeArray), for: .touchUpInside)
        self.addSubview(randomizeArrayButton)
        
        insertionSortButton.frame = CGRect(x: 135, y: 45, width: 100, height: 40)
        insertionSortButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        insertionSortButton.setTitle("Insertion Sort", for: .normal)
        insertionSortButton.setTitleColor(UIColor.red, for: .disabled)
        insertionSortButton.setTitleColor(UIColor.black, for: .normal)
        insertionSortButton.layer.masksToBounds = true
        insertionSortButton.layer.borderWidth = 1
        insertionSortButton.layer.borderColor = UIColor.black.cgColor
        insertionSortButton.addTarget(self, action: #selector(insertionSortAction), for: .touchUpInside)
        self.addSubview(insertionSortButton)
        
        selectionSortButton.frame = CGRect(x: 240, y: 45, width: 105, height: 40)
        selectionSortButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        selectionSortButton.setTitle("Selection Sort", for: .normal)
        selectionSortButton.setTitleColor(UIColor.black, for: .normal)
        selectionSortButton.setTitleColor(UIColor.red, for: .disabled)
        selectionSortButton.layer.masksToBounds = true
        selectionSortButton.layer.borderWidth = 1
        selectionSortButton.layer.borderColor = UIColor.black.cgColor
        self.addSubview(selectionSortButton)
        selectionSortButton.addTarget(self, action: #selector(selectionSortAction), for: .touchUpInside)
        
        bubbleSortButton.frame = CGRect(x: 350, y: 45, width: 90, height: 40)
        bubbleSortButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        bubbleSortButton.setTitle("Bubble Sort", for: .normal)
        bubbleSortButton.setTitleColor(UIColor.black, for: .normal)
        bubbleSortButton.setTitleColor(UIColor.red, for: .disabled)
        bubbleSortButton.layer.masksToBounds = true
        bubbleSortButton.layer.borderWidth = 1
        bubbleSortButton.layer.borderColor = UIColor.black.cgColor
        bubbleSortButton.addTarget(self, action: #selector(bubbleSortAction), for: .touchUpInside)
        self.addSubview(bubbleSortButton)
        
        quickSortButton.frame = CGRect(x: 445, y: 45, width: 90, height: 40)
        quickSortButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        quickSortButton.setTitle("Quick Sort", for: .normal)
        quickSortButton.setTitleColor(UIColor.black, for: .normal)
        quickSortButton.setTitleColor(UIColor.red, for: .disabled)
        quickSortButton.layer.masksToBounds = true
        quickSortButton.layer.borderWidth = 1
        quickSortButton.layer.borderColor = UIColor.black.cgColor
        quickSortButton.addTarget(self, action: #selector(quickSortAction), for: .touchUpInside)
        self.addSubview(quickSortButton)
        
        mergeSortButton.frame = CGRect(x: 540, y: 45, width: 90, height: 40)
        mergeSortButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        mergeSortButton.setTitle("Merge Sort", for: .normal)
        mergeSortButton.setTitleColor(UIColor.black, for: .normal)
        mergeSortButton.setTitleColor(UIColor.red, for: .disabled)
        mergeSortButton.layer.masksToBounds = true
        mergeSortButton.layer.borderWidth = 1
        mergeSortButton.layer.borderColor = UIColor.black.cgColor
        mergeSortButton.addTarget(self, action: #selector(mergeSortAction), for: .touchUpInside)
        self.addSubview(mergeSortButton)
        
        shellSortButton.frame = CGRect(x: 635, y: 45, width: 90, height: 40)
        shellSortButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        shellSortButton.setTitle("Shell Sort", for: .normal)
        shellSortButton.setTitleColor(UIColor.black, for: .normal)
        shellSortButton.setTitleColor(UIColor.red, for: .disabled)
        shellSortButton.layer.masksToBounds = true
        shellSortButton.layer.borderWidth = 1
        shellSortButton.layer.borderColor = UIColor.black.cgColor
        shellSortButton.addTarget(self, action: #selector(shellSortAction), for: .touchUpInside)
        self.addSubview(shellSortButton)
        
//        changeSizeButton.frame = CGRect(x: 775, y: 45, width: 90, height: 40)
//        changeSizeButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
//        changeSizeButton.setTitle("Change Size", for: .normal)
//        changeSizeButton.setTitleColor(UIColor.black, for: .normal)
//        changeSizeButton.layer.masksToBounds = true
//        changeSizeButton.layer.borderWidth = 1
//        changeSizeButton.layer.borderColor = UIColor.black.cgColor
//        self.addSubview(changeSizeButton)
        
        setLabelArray()
        
    }
    //    生成随机数组
    @objc func randomizeArray(){
        
        if randomizeArrayButton.tag == 1{
           randomizeButtonRestoreAction()
        }
        
        
        for i in labelArray{
            i.removeFromSuperview()
        }
        labelArray.removeAll()
        setLabelArray()
    }
    //    创建label数组
    func setLabelArray(){
        for i in 0...Int(labelArrayCount-2){
            let random = arc4random()%99 + 1
            let fram = CGRect(x: Int(5+25*i), y: 100, width: 20, height: 130)
            let label = AccordingToHeightLabel(frame: fram, heightInt: Int(random))
            self.addSubview(label)
            labelArray.append(label)
        }
    }
    // 插入排序操作---------------------------------------
    @objc func insertionSortAction(){
        allButtonAction()
        
        outerLoopVariable = 1
        withinLoopVariable = outerLoopVariable
        time = Timer.scheduledTimer(timeInterval: sumTime, target: self, selector: #selector(insertionSortActionTime), userInfo: nil, repeats: true)
        
    }
    @objc func insertionSortActionTime() {
        
        for i in labelArray{
            i.changeColor(type: .normal)
        }
        if withinLoopVariable>=1{
            labelArray[withinLoopVariable].changeColor(type: .highlighted)
            labelArray[withinLoopVariable-1].changeColor(type: .highlighted)
        }
        if withinLoopVariable-1 >= 0{
            if labelArray[withinLoopVariable].heightInt < labelArray[withinLoopVariable-1].heightInt{
                
                let fram = labelArray[withinLoopVariable].frame
                let labe = labelArray[withinLoopVariable]
                
                UIView.animate(withDuration: sumTime, animations: {
                    self.labelArray[self.withinLoopVariable].frame = self.labelArray[self.withinLoopVariable-1].frame
                    self.labelArray[self.withinLoopVariable-1].frame = fram
                }) { (_) in
                    
                }
                labelArray[withinLoopVariable] = labelArray[withinLoopVariable-1]
                labelArray[withinLoopVariable-1] = labe
            }
            withinLoopVariable = withinLoopVariable-1
        }else{
            outerLoopVariable = outerLoopVariable+1
            withinLoopVariable = outerLoopVariable
        }
        if outerLoopVariable >= labelArray.count{
            time.invalidate()
            print("jieshu")
            outerLoopVariable = 0
            withinLoopVariable = 0
        }
    }
    //    选择排序操作过程---------------------------------------
    @objc func selectionSortAction(){
        allButtonAction()
        outerLoopVariable = 0
        withinLoopVariable = outerLoopVariable + 1
        intermediateVariableLabel = labelArray[outerLoopVariable]
        
        time = Timer.scheduledTimer(timeInterval: sumTime, target: self, selector: #selector(selectionSortActionTime), userInfo: nil, repeats: true)
    }
    @objc func selectionSortActionTime(){
        if outerLoopVariable < labelArray.count{
            if withinLoopVariable < labelArray.count{
                for i in labelArray{
                    i.changeColor(type: .normal)
                }
                labelArray[withinLoopVariable].changeColor(type: .highlighted)
                intermediateVariableLabel.changeColor(type: .highlighted)
                if labelArray[withinLoopVariable].heightInt < intermediateVariableLabel.heightInt{
                    intermediateVariableLabel = labelArray[withinLoopVariable]
                }
                withinLoopVariable = withinLoopVariable + 1
            }else{
                swapLabel()
                outerLoopVariable = outerLoopVariable + 1
                withinLoopVariable = outerLoopVariable + 1
                if withinLoopVariable <= labelArray.count{
                    intermediateVariableLabel = labelArray[outerLoopVariable]
                }
            }
        }else{
            time.invalidate()
            for i in labelArray{
                i.backgroundColor = UIColor.clear
            }
            print("结束")
        }
    }
    
    func swapLabel(){
        //交换 label 在视图上的位置
        let intermediateVariableFram = intermediateVariableLabel.frame
        UIView.animate(withDuration: sumTime) {
            self.intermediateVariableLabel.frame = self.labelArray[self.outerLoopVariable].frame
            self.labelArray[self.outerLoopVariable].frame = intermediateVariableFram
        }
        // 交换 label 在数组中的位置
        let intermediateVariableLab = labelArray[getSubscript()]
        labelArray[getSubscript()] = labelArray[outerLoopVariable]
        labelArray[outerLoopVariable] = intermediateVariableLab
    }
    // 得到label中间变量 在label数组中的下标位置
    func getSubscript() -> Int{
        var subscriptLabel : Int!
        for i in 0..<labelArray.count{
            if intermediateVariableLabel == labelArray[i]{
                subscriptLabel = i
            }
        }
        return subscriptLabel
    }
    //冒泡 排序搜索过程----------------------------------------------------
    @objc func bubbleSortAction(){
        allButtonAction()
        outerLoopVariable = 0
        withinLoopVariable = 0
        time = Timer.scheduledTimer(timeInterval: sumTime, target: self, selector: #selector(bubbleSortActionTime), userInfo: nil, repeats: true)
    }
    @objc func bubbleSortActionTime(){
        if outerLoopVariable < labelArray.count{
            if withinLoopVariable < labelArray.count - (outerLoopVariable+1){
                for i in labelArray{
                    i.changeColor(type: .normal)
                }
                labelArray[withinLoopVariable].changeColor(type: .highlighted)
                labelArray[withinLoopVariable+1].changeColor(type: .highlighted)
                if labelArray[withinLoopVariable].heightInt > labelArray[withinLoopVariable+1].heightInt{
                    let fram = labelArray[withinLoopVariable].frame
                    UIView.animate(withDuration: sumTime) {
                        self.labelArray[self.withinLoopVariable].frame = self.labelArray[self.withinLoopVariable+1].frame
                        self.labelArray[self.withinLoopVariable+1].frame = fram
                    }
                    let labe = labelArray[withinLoopVariable]
                    labelArray[withinLoopVariable] = labelArray[withinLoopVariable+1]
                    labelArray[withinLoopVariable+1] = labe
                }
                withinLoopVariable = withinLoopVariable + 1
            }else{
                outerLoopVariable = outerLoopVariable + 1
                withinLoopVariable = 0
            }
        }else{
            time.invalidate()
            print("结束啦")
            for i in labelArray{
                i.backgroundColor = UIColor.clear
            }
            withinLoopVariable = 0
            outerLoopVariable = 0
        }
    }
  //快速排序操作过程------------------------------------------------------
    
    @objc func quickSortAction(){
        allButtonAction()
        signal = DispatchSemaphore(value:0)
        time = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(quickSortTimerAction), userInfo: nil, repeats: true)
        DispatchQueue.global().async {
            self.qSort(arr: &self.labelArray, left: 0, right: self.labelArray.count-1)
        }
    }
    
    @objc func quickSortTimerAction(){
        if parametersOfTheArray.count == 3{
            for i in labelArray{
                i.changeColor(type: .normal)
            }
            label.frame = CGRect(x: 0, y: 210-labelArray[Int(parametersOfTheArray[0])].heightInt, width: Int(UIScreen.main.bounds.width), height: 1)
            labelArray[Int(parametersOfTheArray[1])].changeColor(type: .highlighted)
            labelArray[Int(parametersOfTheArray[2])].changeColor(type: .highlighted)
            sleep(UInt32(sumTime))
            UIView.animate(withDuration:sumTime, animations: {
                let fram = self.labelArray[Int(self.parametersOfTheArray[1])].frame
                self.labelArray[Int(self.parametersOfTheArray[1])].frame = self.labelArray[Int(self.parametersOfTheArray[2])].frame
                self.labelArray[Int(self.parametersOfTheArray[2])].frame = fram
                self.parametersOfTheArray.removeAll()
            }) { (_) in
                self.signal.signal()
            }
        }else if parametersOfTheArray.count == 4{
            for i in labelArray{
                i.changeColor(type: .normal)
            }
            label.frame = CGRect(x: 0, y: 210-labelArray[Int(parametersOfTheArray[0])].heightInt, width: Int(UIScreen.main.bounds.width), height: 1)
            labelArray[Int(parametersOfTheArray[0])].changeColor(type: .disable)
            labelArray[Int(parametersOfTheArray[1])].changeColor(type: .highlighted)
            labelArray[Int(parametersOfTheArray[2])].changeColor(type: .highlighted)
            parametersOfTheArray.removeAll()
            sleep(UInt32(sumTime))
            self.signal.signal()
            
        }
    }
    
    func qSort(arr:inout [AccordingToHeightLabel],left: Int,right: Int){
        if left > right{
            return
        }
        
        let base = arr[left]//定义变量保存基准数
        var i = left//定义变量i指向最左边
        var j = right//定义变量j，指向最右边
        
        while i != j {
            //j从右往左检索比基准数小的，如果检索到比基准数小的就停下
            //如果检索到比基准数大的或者相等的，经继续检索
            while(arr[j].heightInt >= base.heightInt && i < j){
                parametersOfTheArray.removeAll()
                parametersOfTheArray.append(Double(left))
                parametersOfTheArray.append(Double(i))
                parametersOfTheArray.append(Double(j))
                parametersOfTheArray.append(1000)
                signal.wait()
                //                signal.wait()
                j = j - 1 //j从右向左移动
            }
            while(arr[i].heightInt <= base.heightInt && i < j ){
                
                parametersOfTheArray.removeAll()
                parametersOfTheArray.append(Double(left))
                parametersOfTheArray.append(Double(i))
                parametersOfTheArray.append(Double(j))
                parametersOfTheArray.append(1000)
                signal.wait()
                //                signal.wait()
                i = i + 1 //i从左往右移动
            }
            //jiaohuanla交换
            
            parametersOfTheArray.removeAll()
            parametersOfTheArray.append(Double(left))
            parametersOfTheArray.append(Double(i))
            parametersOfTheArray.append(Double(j))
            signal.wait()
            let la = arr[i]
            arr[i] = arr[j]
            arr[j] = la
            
        }
        //如果上面while循环的条件不成立，会跳出这个循环 往下执行
        //说明i和j相遇了   交换基准数和这个元素相遇的元素
        parametersOfTheArray.removeAll()
        parametersOfTheArray.append(Double(left))
        parametersOfTheArray.append(Double(left))
        parametersOfTheArray.append(Double(i))
        signal.wait()
        arr[left] = arr[i]
        arr[i] = base
        
        qSort(arr: &arr, left: left, right: i-1)
        qSort(arr: &arr, left: j + 1, right: right)
        
    }
    
    
    
    //归并排序操作过程----------------------------------------------------
    @objc func mergeSortAction(){
        allButtonAction()
      
        signal = DispatchSemaphore(value:0)
        time = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(meegeSortTimeAction), userInfo: nil, repeats: true)
        
        DispatchQueue.global().async {
            self.mergeSort(arr: &self.labelArray, low: 0, high: self.labelArray.count-1)
        }
    }
//    计时器没有结束 问题 很大很大记得改改改==========+++++++===================
    //    计时器没有结束 问题很大很大记得改改改==========+++++++===================
    
    @objc func meegeSortTimeAction(){
        if self.parametersOfTheArray.count == 3{
            
            DispatchQueue.main.async {
                UIView.animate(withDuration: self.sumTime, animations: {
                    self.labelArray[Int(self.parametersOfTheArray[0])].center = CGPoint(x: self.parametersOfTheArray[1], y: self.parametersOfTheArray[2])
                    
                    self.parametersOfTheArray.removeAll()
                    
                    
                }, completion: { (_) in
                    self.signal!.signal()
                })
            }
        }
    
    }
    
    func mergeSort(arr:inout [AccordingToHeightLabel],low: Int,high: Int){
        if (low >= high){
            return
        }
        //        let mid = low + (high - low)/2
        let mid = (low + high) / 2
        mergeSort(arr: &arr, low: low, high: mid)
        mergeSort(arr: &arr, low: mid + 1, high: high)
        merge(arr: &arr, low: low, mid: mid, high: high)
        
    }
    //组合--------------
    func merge(arr:inout [AccordingToHeightLabel],low: Int,mid: Int,high: Int){
        let count:Int = high - low + 1
        var temp:[Any] = [Any].init(repeating: 0, count: count)
        var i:Int = low //左指针
        var j:Int = mid+1//右指针
        var index:Int = 0
        
        
        while i <= mid && j <= high {
            if arr[i].heightInt < arr[j].heightInt{
                temp[index] = arr[i]
                parametersOfTheArray.removeAll()
                parametersOfTheArray.append(Double(i))
                parametersOfTheArray.append(Double(15 + 25*(low+index)))
                parametersOfTheArray.append(240)
                signal!.wait()
                i += 1
            }else{
                temp[index] = arr[j]
                parametersOfTheArray.removeAll()
                parametersOfTheArray.append(Double(j))
                parametersOfTheArray.append(Double(15 + 25*(low+index)))
                parametersOfTheArray.append(240)
                signal!.wait()
                j += 1
            }
            index += 1
        }
        while i <= mid {
            temp[index] = arr[i]
            parametersOfTheArray.removeAll()
            parametersOfTheArray.append(Double(i))
            parametersOfTheArray.append(Double(15 + 25*(low+index)))
            parametersOfTheArray.append(240)
            signal!.wait()
            i += 1
            index += 1
        }
        while j <= high {
            temp[index] = arr[j]
            parametersOfTheArray.removeAll()
            parametersOfTheArray.append(Double(j))
            parametersOfTheArray.append(Double(15 + 25*(low+index)))
            parametersOfTheArray.append(240)
            signal!.wait()
            j += 1
            index += 1
        }
        for m in 0..<count{
            arr[low+m] = temp[m] as! AccordingToHeightLabel
            parametersOfTheArray.removeAll()
            parametersOfTheArray.append(Double(low+m))
            parametersOfTheArray.append(Double(15 + 25*(low+m)))
            parametersOfTheArray.append(165)
            
            signal!.wait()
        }
    }
    
    
    
    
    
    //希尔排序操作过程-----------------------------------------------------
    @objc func shellSortAction(){
        allButtonAction()
        gap = labelArray.count/2
        newLabelArray = getLabelArray(gat: gap, number: 0)
        
        outerLoopVariable = 1
        withinLoopVariable = outerLoopVariable
        time = Timer.scheduledTimer(timeInterval: sumTime, target: self, selector: #selector(shellSortActionSwap), userInfo: nil, repeats: true)
    }
   @objc func shellSortActionSwap(){
    
                for i in labelArray{
                    i.changeColor(type: .disable)
                }
                for i in newLabelArray{
                    i.changeColor(type: .normal)
                }
    
                if self.withinLoopVariable >= 1{
                    self.newLabelArray[self.withinLoopVariable].changeColor(type: .highlighted)
                    self.newLabelArray[self.withinLoopVariable-1].changeColor(type: .highlighted)
                }
    
                if self.withinLoopVariable-1 >= 0 {
                    if self.newLabelArray[self.withinLoopVariable].heightInt < self.newLabelArray[self.withinLoopVariable-1].heightInt{
                        
                        UIView.animate(withDuration: sumTime) {
                            let fram = self.newLabelArray[self.withinLoopVariable].frame
                            self.newLabelArray[self.withinLoopVariable].frame = self.newLabelArray[self.withinLoopVariable-1].frame
                            self.newLabelArray[self.withinLoopVariable-1].frame = fram
                        }

                        let la = newLabelArray[withinLoopVariable]
                        newLabelArray[withinLoopVariable] = newLabelArray[withinLoopVariable-1]
                        newLabelArray[withinLoopVariable-1] = la
                        
                        let lab = self.labelArray[withinLoopVariable*gap+shellnum]
                        self.labelArray[withinLoopVariable*gap+shellnum] = self.labelArray[(withinLoopVariable-1)*gap+shellnum]
                        self.labelArray[(withinLoopVariable-1)*gap+shellnum] = lab
                    }
                    self.withinLoopVariable = self.withinLoopVariable-1
                }else{
                    self.outerLoopVariable = self.outerLoopVariable+1
                    self.withinLoopVariable = self.outerLoopVariable
                }
    
            if self.outerLoopVariable >= self.newLabelArray.count{
                outerLoopVariable = 1
                withinLoopVariable = outerLoopVariable
                 self.shellnum = self.shellnum + 1
                if self.shellnum<self.gap{
                    self.newLabelArray.removeAll()
                    self.newLabelArray = self.getLabelArray(gat: self.gap, number: self.shellnum)
                }else{
                    self.gap = self.gap/2
                    self.shellnum = 0
                    self.newLabelArray.removeAll()
                    self.newLabelArray = self.getLabelArray(gat: self.gap, number: self.shellnum)
                }
            }
                if self.gap<=0{
                    self.time.invalidate()
                    print("搞定")
                }
    
 
    }
    //希尔排序操作得到新的数组------------------------------------------------------
    func getLabelArray(gat: Int,number: Int) -> [AccordingToHeightLabel]{
        var array = [AccordingToHeightLabel]()
        if number < gat {
            for i in number...labelArray.count-1{
                if i%gat == number{
                    array.append(labelArray[i])
                }
            }
        }
        return array
    }

    

    
    func allButtonAction(){
        insertionSortButton.isEnabled = false
        selectionSortButton.isEnabled = false
        bubbleSortButton.isEnabled = false
        quickSortButton.isEnabled = false
        mergeSortButton.isEnabled = false
        shellSortButton.isEnabled = false
        slider.isEnabled = false
        randomizeArrayButton.tag = 1
    }
    
     func randomizeButtonRestoreAction(){
        insertionSortButton.isEnabled = true
        selectionSortButton.isEnabled = true
        bubbleSortButton.isEnabled = true
        quickSortButton.isEnabled = true
        mergeSortButton.isEnabled = true
        shellSortButton.isEnabled = true
        slider.isEnabled = true
        randomizeArrayButton.tag = 0
        label.frame = CGRect(x: 0, y: -10, width: UIScreen.main.bounds.width, height: 1)
        
        if time != nil{
            time.invalidate()
        }
        if signal != nil{
            signal = nil
        }
    }
    
    

    

//控制每一步动画总时间---------------------------------------------------
    @objc func ActionSliderValueChange(){
        if Double(slider.value) <= 0.0001{
            sumTime = 0.0001
        }else{
            sumTime = Double(slider.value)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}




